package com.shuaiche.yeguan.util;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by baoyinbo on 2017/12/29.
 * 数字 监听小数点后几位有效数，自动截取
 *
 */

public class SCEditTextPointUtils {
    /**
     *
     * @param editText
     * @param num   保留几位有效数
     * @param maxNum   不能大于这个数； 0:默认
     */
    public static void setNumPoint(final EditText editText, final int num, final float maxNum) {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > num) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + num + 1);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }

                try {
                    if (maxNum > 0 && s.length() > 0) {
                        if (s.toString().equals("null") ) {
                            editText.setText(null);
                        } else if (Float.parseFloat(s.toString().replace(",", "")) > maxNum ) {
                            ToastShowUtils.showTipToast("您输入的数字已超过最大值");
                            if (s.toString().contains(".")) {
//                            int pointIndex = s.toString().indexOf(".");
                                s = s.toString().subSequence(0, s.toString().indexOf(".") - 1);
                                editText.setText(s);
                                editText.setSelection(s.length());
                            } else {
                                //不包含小数
                                s = s.toString().subSequence(0, s.length() - 1);
                                editText.setText(s);
                                editText.setSelection(s.length());
                            }
                        }
                    }
                } catch (Exception e) {

                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }

        });

    }

    public static void setNumPoint(final EditText editText, final int num) {
        setNumPoint(editText, num, 214748); //int 最大值
    }


    /**
     * 字符串长度控制
     * @param editText
     * @param maxLength
     */
    public static void setStringLength(final EditText editText, final int maxLength) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (maxLength > 0) {
                    if (s.toString().length() > maxLength ) {
                        ToastShowUtils.showTipToast("长度不能大于" + maxLength + "位");
                        s = s.toString().subSequence(0, maxLength);
                        editText.setText(s);
                        editText.setSelection(s.length());

                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }

        });
    }


    public static void setStringLength(final EditText editText, final int maxLength, final TextView tv) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (maxLength > 0) {
                    if (s.toString().length() > maxLength ) {
                        ToastShowUtils.showTipToast("长度不能大于" + maxLength + "位");
                        s = s.toString().subSequence(0, maxLength);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }

                    tv.setText(s.toString().length() + "/" + maxLength);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }

        });
    }
}
